# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-system
_pkgname=luasystem
pkgver=0.2.1
pkgrel=2
pkgdesc="Platform independent system calls for Lua"
url="https://github.com/o-lim/luasystem/"
arch="all"
license="MIT"
source="$pkgname-$pkgver.tar.gz::https://github.com/o-lim/$_pkgname/archive/v$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"
# This module needs lua-busted to run tests, but lua-busted depends on
# lua-system and we wanna avoid circular dependencies.
options="!check"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building against Lua $lver..."
		make -C "$builddir-$lver"/src \
			LUA_VERSION=$lver \
			MYCFLAGS="$CFLAGS"
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc $lver"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local lmod_dir=$(pkg-config --variable=INSTALL_LMOD lua$lver)
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-0"

	cd "$builddir-$lver"

	make -C src LUA_VERSION=$lver DESTDIR="$subpkgdir" prefix=/usr \
		install
	install -m 644 -D system/init.lua "$subpkgdir"/$lmod_dir/system.lua

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="b8a04a83e8e39c98da23484c9eba9ff60da2fb4db3deca0d52784a6c13c7e0faf7a8b326999d88703847107e4fce739062fc04a00266140969c9f57b70064f46  lua-system-0.2.1.tar.gz"
